package com.neulogistics.contrib.procuredcenter.api.application.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neulogistics.contrib.procuredcenter.api.application.converter.GoodsListPOVMConverter;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.ClassifyViewModel;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.GoodsListViewModel;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.GoodsListPO;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.GoodsSetPO;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.service.IGoodsListPOService;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.service.IGoodsSetPOService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsListQueryService implements IGoodsListQueryService {
    @Resource
    IGoodsListPOService goodsListPOService;

    @Resource
    IGoodsSetPOService goodsSetPOService;

    @Override
    public GoodsListViewModel getById(Integer listId) {
        GoodsListPO goodsListPO = goodsListPOService.getById(listId);
        GoodsListViewModel goodsListViewModel = GoodsListPOVMConverter.POToVM(goodsListPO);
        return goodsListViewModel;
    }

    @Override
    public ClassifyViewModel getBySetId(Integer setId) {
        GoodsSetPO setPO = goodsSetPOService.getById(setId);
        LambdaQueryWrapper<GoodsListPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsListPO::getListId, setPO.getListId());
        GoodsListPO listPO = goodsListPOService.getOne(queryWrapper);

        ClassifyViewModel classifyViewModel = new ClassifyViewModel();
        classifyViewModel.setListId(listPO.getListId());
        classifyViewModel.setListName(listPO.getListName());
        classifyViewModel.setSetId(setId);
        classifyViewModel.setSetName(setPO.getSetName());
        return classifyViewModel;
    }

    @Override
    public List<GoodsListViewModel> list() {

        List<GoodsListPO> list = goodsListPOService.list();
        List<GoodsListViewModel> viewModelList = list.stream().map(GoodsListPOVMConverter::POToVM).collect(Collectors.toList());

        return viewModelList;
    }
}
